<?php
class LoggerUtil {

  private static $logFileName = 'system.log';

  private static $logger = null;

  public static function addEntry($message, $type = sfLogger::INFO, $depth = 0) {
    // Maintain the log object as a singleton
    if (is_null(self::$logger)) {
      try {
        self::$logFileName = str_replace('.log', '-' . date('dmY') . '.log', self::$logFileName);
        self::$logger = new sfFileLogger(new sfEventDispatcher(), array('file' =>
                        sfConfig::get('sf_root_dir') . '/log/' . self::$logFileName));
        if (strtolower(sfConfig::get('app_common_log_debug_level')) === 'on') {
          self::$logger->setLogLevel('DEBUG');
        }
      } catch (Exception $e) {
        echo $e->getMessage();
      }
    }

    // Get the users IP address
    $ipAddress = sfContext::getInstance()->getRequest()->getHttpHeader('addr', 'remote');

    // Only for error type log entries we include the filename and line no. to make debugging easier
    $fileInfo = '';
    if ($type == sfLogger::ERR) {
      $trace = debug_backtrace();
      if (isset($trace[$depth])) {
        $fileInfo = ' [' . str_replace(sfConfig::get('sf_root_dir') . '/', '', $trace[$depth]['file']) . ':' .
                $trace[$depth]['line'] . ']';
      }
    }

    $message = '[' . $ipAddress . ']' . $fileInfo . ' ' . $message;

    // Write log file
    self::$logger->log($message, $type);
  }
}